package com.knight.hadoop.day05.springannotation.userdefinedannotation.test;

import java.lang.reflect.Method;
import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import com.knight.hadoop.day05.springannotation.userdefinedannotation.annotation.RpcService;


@Component
public class MyServer implements ApplicationContextAware {
	public static void main(String[] args) {
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

		Map<String, Object> objectMap = applicationContext.getBeansWithAnnotation(RpcService.class);
 
		for (Object serviceBean : objectMap.values()) {
			
			String annotationValue = serviceBean.getClass().getAnnotation(RpcService.class).value();
			System.out.println("RpcService value: "+annotationValue);
			
			try {
				Method method = serviceBean.getClass().getMethod("hello", new Class<?>[]{String.class});
				Object result = method.invoke(serviceBean, "kkkk");
				System.out.println("hello method result: "+result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
	}
}
